import { ref } from "vue";

export const useAsyncData = (op) => {
  const { APIS, table, filterForm = {}, formatList } = op;
  const queryParams = ref({});

  const fetchData = (pageForm, done) => {
    return APIS.query({
      pageNum: pageForm.page,
      pageSize: pageForm.size,
	    governPlanStatus: 1,
      ...queryParams.value,
      ...filterForm,
    }).then((res) => {

      if (formatList) {
        res.rows = formatList(res.rows || res.data) || res.rows || res.data;
      }
      done({
        list: res.rows || res.data,
        total: res.total,
      });
      return res.rows || res.data;
    });
  };

  const getList = (flag) => {
    if (table.value && table.value.refresh) {
      return table.value.refresh(flag);
    }
  };

  return {
    fetchData,
    getList,
    queryParams,
  };
};
